package com.hollycrm.unicom.usersupport.hibernate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import net.sf.hibernate.Session;

import org.springframework.orm.hibernate.HibernateTemplate;

import com.hollycrm.springframework.factory.DefaultBeanFactory;
import com.hollycrm.unicom.usersupport.Department;
import com.hollycrm.unicom.usersupport.DepartmentTreeViewObject;
import com.hollycrm.unicom.usersupport.User;

/**
 * HibernateUserSupportHelper
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Tian Kai
 * @version 1.0
 */
public class HibernateUserSupportHelper {

    public final static String BEAN_ID = "hibernateUserSupportHelper";

    public static HibernateUserSupportHelper getInstance() {
        return (HibernateUserSupportHelper) DefaultBeanFactory.getBean(BEAN_ID);
    }

    public final static String QUERY_DEPT_TREE_LIST =
        "select new com.hollycrm.unicom.usersupport.DepartmentTreeViewObject(dept.deptID, dept.deptType, dept.deptID, dept.deptName, dept.leaf, dept.level)" +
        " from dept in " +
        Department.class +" where dept.parentID = :parentID and dept.valid = '1'";

    public final static String QUERY_DEPT_USERS_LIST =
        "select usr from usr in " +
        User.class +" join usr.depts dept where dept.deptID = :deptID and dept.valid = '1'";

    /**
     * getDepartmentTreeList
     *
     * @param session Session
     * @param ht HibernateTemplate
     * @throws HibernateException
     * @return List
     */
    public List getDepartmentTreeList(Session session,HibernateTemplate ht) throws
        HibernateException {
        return getDepartmentTreeList(session, ht, "0", null);
    }

    /**
     * getDepartmentTreeList
     * @param session Session
     * @param ht HibernateTemplate
     * @param parentID String
     * @throws HibernateException
     * @return List
     */
    public List getDepartmentTreeList(Session session, HibernateTemplate ht,String parentID) throws
        HibernateException {
        return getDepartmentTreeList(session, ht, parentID, null);
    }

    /**
     * getDepartmentTreeList
     *
     * @param session Session
     * @param ht HibernateTemplate
     * @param parentID String
     * @param deptType String
     * @throws HibernateException
     * @return List
     */
    public List getDepartmentTreeList(Session session, HibernateTemplate ht, String parentID, String deptType) throws
        HibernateException {
        List tree = new ArrayList();
        Query query = ht.createQuery(session,
                                     QUERY_DEPT_TREE_LIST +
                                     (deptType == null ? "" :
                                      " and dept.deptType='" + deptType + "'"));
        getTreeRecursive(query, tree, parentID);
        return tree;
    }

    public void getTreeRecursive(Query query, List tree, String parentID) throws
        HibernateException {
        query.setString("parentID", parentID);
        List subProblems = query.list();
        Iterator iter = subProblems.iterator();
        List treeList = new ArrayList();
        while (iter.hasNext()) {
            DepartmentTreeViewObject dtvo = (DepartmentTreeViewObject) iter.
                next();
            String deptID = dtvo.getDepartmentID();
            treeList.add(dtvo);
            getTreeRecursive(query, treeList, deptID);
        }
        tree.addAll(treeList);
    }

    /**
     * getDeptUsers
     * @param session Session
     * @param ht HibernateTemplate
     * @param deptID String
     * @throws HibernateException
     * @return List
     */
    public List getDeptUsers(Session session,HibernateTemplate ht,String deptID) throws HibernateException{
        Query query = ht.createQuery(session, QUERY_DEPT_USERS_LIST);
        query.setString("deptID",deptID);
        return query.list();
    }

    /**
     * getDeptChildren
     * @param session Session
     * @param ht HibernateTemplate
     * @param deptID String
     * @throws HibernateException
     * @return List
     */
    public List /*DepartmentTreeViewObject*/ getDeptChildren(Session
        session, HibernateTemplate ht, String deptID) throws
        HibernateException {
        return getDeptChildren(session, ht, deptID, null);
    }

    /**
     * getDeptChildren
     *
     * @param session Session
     * @param ht HibernateTemplate
     * @param deptID String
     * @param deptType String
     * @throws HibernateException
     * @return List
     */
    public List /*DepartmentTreeViewObject*/ getDeptChildren(Session
        session, HibernateTemplate ht, String deptID, String deptType) throws
        HibernateException {
        Query query = ht.createQuery(session,
                                     QUERY_DEPT_TREE_LIST +
                                     (deptType == null ? "" :
                                      " and dept.deptType='" + deptType + "'"));
        query.setString("parentID", deptID);
        return query.list();
    }

    /**
     * retrieveDepartment
     *
     * @param session Session
     * @param ht HibernateTemplate
     * @param deptID String
     * @throws HibernateException
     * @return Department
     */
    public Department retrieveDepartment(Session session, HibernateTemplate ht,
                                         String deptID) throws
        HibernateException {
        return (Department) ht.load(Department.class, deptID);
    }
}
